"use strict";
    //  cinematize widget.js of addon-sdk-1.12.zip
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm")
    //mponents.utils.import("resource://gre/modules/Services.jsm")  //  _dvk_dbg_

var EXPORTED_SYMBOLS = [
        "removeNodeInToolbar",
        "nameCustomizeDlg",
        "pathCustomizeDlg",
        "upgradeBtns",
        "insertBtns" ]

const nameCustomizeDlg = "CustomizeToolbar"
const pathCustomizeDlg = "/content/customizeToolbar.xul"
const xulUnderlay   = "chrome://disallowscriptbtn/content/underlay.xul"
const toolbarbtns   = [
        "#disallowscriptbtn-key-dot",
        "#disallowscriptbtn-plugin-widget",
        "#extra-reload-button",
        "#extra-stop-button"
    ]
const thequeries = [
        "toolbarbutton#disallowscriptbtn-key-dot",
        "toolbarbutton#disallowscriptbtn-plugin-widget",
        "toolbarbutton#extra-reload-button",
        "toolbarbutton#extra-stop-button"
    ]

function upgradeBtns(awin)
{
    try {
        upgradeNodeInToolbar(awin, "disallow-script-btn", "disallowscriptbtn-key-dot");
        upgradeNodeInToolbar(awin, "plugin-mode-widget", "disallowscriptbtn-plugin-widget");
    }
    catch (err)
    {
        Components.utils.reportError(err)
    }
}

function insertBtns(awin)
{
    try {
        insertNodeInToolbar(awin, "disallowscriptbtn-plugin-widget", true);
        insertNodeInToolbar(awin, "disallowscriptbtn-key-dot", false);
    }
    catch (err)
    {
        Components.utils.reportError(err)
    }
}

function applyToolbox(atoolbox, asbn, aproc)
{
    let templateNode = atoolbox.palette.firstChild;
    while(templateNode)
        if(templateNode.id == asbn)
        {
            aproc(templateNode);
            templateNode = null;
        }
        else
            templateNode = templateNode.nextSibling;
}

//  _insertNodeInToolbar: function BW__insertNodeInToolbar(node) of add-on-sdk
function insertNodeInToolbar(awin, asbn, aforce)
{
    // Search for widget toolbar by reading toolbar's currentset attribute
    let container = null;
    let apersist = awin.gNavToolbox.customizing;
    let toolbars = awin.document.getElementsByTagName("toolbar");
    for (let i = 0, l = toolbars.length; i < l; ++i)
        if (toolbars[i].getAttribute("currentset").indexOf(asbn) + 1)
        {
            container = toolbars[i];
            if(!(toolbars[i].collapsed)) break;
        }

    if(!(awin.CustomizableUI) && aforce)
    {
        let thebar = awin.document.getElementById("addon-bar");
        if(!container && thebar) container = thebar;

        if(container)   //  -plugin-widget
        if(container === thebar)
            if(container.collapsed)
                awin.toggleAddonBar();
    }

    let theobj = awin.CustomizableUI;
    if (!container && theobj)
    try {
        let thestr = (aforce) ? theobj.AREA_PANEL : theobj.AREA_NAVBAR;
        theobj.addWidgetToArea(asbn, thestr);
    }
    catch(err)
    {
        Components.utils.reportError(err)
    }

    if (!container) return; //  or already exists
    if (container.collapsed) awin.setToolbarVisibility(container, true);
    if (container.querySelector([ "toolbarbutton", asbn ].join("#"))) return;

    let nextNode = null;
    let index = (-1);
    let currentSet = container.getAttribute("currentset");
// Now retrieve a reference to the next toolbar item by reading currentset attribute on the toolbar
    if(currentSet)
    try {
        let ids = (currentSet == "__empty") ? [] : currentSet.split(",");
        index = ids.indexOf(asbn);
        if(apersist && (index + 1))
        for (let i = index; i < ids.length; i++)
        {
            nextNode = awin.document.getElementById(ids[i]);
            if (nextNode) break;
        }
    }
    catch(err)
    {
        Components.utils.reportError(err)
    }
//     container.currentSet = "addonbar-closebutton,spring,status-bar";
//     container.setAttribute("currentset", "addonbar-closebutton,spring,status-bar");
    try {
        container.insertItem(asbn, nextNode, null, false);
        if(apersist && (index == (-1)))
        {
            container.setAttribute("currentset", container.currentSet);
            awin.document.persist(container.id, "currentset");
        }
    }
    catch(err)
    {
        Components.utils.reportError(err)
    }

    return;
}

function loadBtns(awin)
{
    let container = awin.document.getElementById("BrowserToolbarPalette");
    if(!container) return;
    
    let thebtn = container.querySelector("toolbarbutton#disallowscriptbtn-key-dot");
        if(thebtn) awin.gNavToolbox.palette.appendChild(thebtn);

    thebtn = container.querySelector("toolbarbutton#disallowscriptbtn-plugin-widget");
        if(thebtn) awin.gNavToolbox.palette.appendChild(thebtn);
    container.parentNode.removeChild(container);
    return;
}

function removeNodeInToolbar(awin)
{
    toolbarbtns.forEach( function(atoolbarbtn)
        {
            let thestr = [ "toolbarpaletteitem", atoolbarbtn ].join(" > ");
            let thebtn = this.querySelector(thestr);
            if(thebtn)
            {
                thebtn = thebtn.parentNode;
                thebtn.parentNode.removeChild(thebtn);
            }
        }, awin.document);

    toolbarbtns.forEach( function(atoolbarbtn)
        {
            let thebtn = this.querySelector(atoolbarbtn);
            if(thebtn) thebtn.parentNode.removeChild(thebtn);
        }, awin.document);

    if(awin.gNavToolbox)
    thequeries.forEach( function(asbn)
        {
            let thebtn = this.palette.querySelector(asbn);
            if(thebtn) this.palette.removeChild(thebtn);
        }, awin.gNavToolbox);
}

function upgradeNodeInToolbar(awin, anoldval, anewval)
{
    let container = null;
    let toolbars = awin.document.getElementsByTagName("toolbar");
    for (let i = 0, l = toolbars.length; i < l; ++i)
        if(toolbars[i].getAttribute("currentset").indexOf(anoldval) + 1)
        {
            container = toolbars[i];
            break;
        }

    let thequery = [ "toolbarbutton", anoldval ].join("#");
    if(container)
    {
        let thebtn = container.querySelector(thequery);
        if(thebtn)
        {
            container.insertItem(anewval, thebtn, null, false);
            thebtn.parentNode.removeChild(thebtn);
        }
        else
            container.insertItem(anewval, thebtn, null, false);

        let currentSet = container.currentSet;
        container.setAttribute("currentset", currentSet);
//    dump("_dvk_dbg_, currentset:\t"); dump(currentSet); dump("\n");
        awin.document.persist(container.id, "currentset");
    }

    let thebtn = awin.gNavToolbox.palette.querySelector(thequery);
        if(thebtn) awin.gNavToolbox.palette.removeChild(thebtn);
}
